

import { getsn } from "@/utils/getsn"
import { UserInfo, CallNet } from "@/utils/public";

/**
 *
 *
 * @export
 * @param {*} cons_no
 * @param {*} callback
 */
export function userInfoFn(cons_no, callback) {
  AndroidApi.queryTableData(JSON.stringify(
    {
      "whereClause": "SELECT * FROM CUST_ADDRINFO A  WHERE A.CONS_NO=?;",
      "whereArgs": [cons_no]
    }
  ), function (msg) {
    if (msg.result.length > 0) {
      callback(msg.result)
    }
  });
}

/**
 * 查询展示
 *
 * @export
 * @param {*} cons_no 用户编号
 * @param {*} tableName 表名
 */
export function sqlQuery(cons_no, tableName, callback) {
  const changeName = (tableName + "_CHANGE").toUpperCase();
  console.log(changeName);
  console.log(tableName);
  console.log(cons_no);
  AndroidApi.queryTableData(JSON.stringify(
    {
      "whereClause": "SELECT * FROM " + changeName + " A  WHERE A.CONS_NO=?;",
      "whereArgs": [cons_no]
    }
  ), function (msg) {
    console.log(111111111111111,msg);
    if (msg.result.length > 0) {
      callback('change', msg.result)
    }
  });
  AndroidApi.queryTableData(
    JSON.stringify({
      "whereClause": "SELECT *  FROM " + tableName.toUpperCase() + " B WHERE B.CONS_NO=?;",
      "whereArgs": [cons_no]
    }), function (msg) {
      console.log(111111111111111,msg);
      if (msg.result.length > 0) {
        callback('noChange', msg.result)
      }
    });

}
/**
 * 数据库添加数据（保存）
 *
 * @export
 * @param {*} tableName 表名
 * @param {*} tmpList 数组 -> 表中的字段[{name:''123}]
 * @param {*} callback 回掉函数
 */
export function sqlInsert(tableName, tmpList, callback) {
  AndroidApi.insert(JSON.stringify({
    "jToken": "",
    "tableName": tableName,
    "contentValues": tmpList
  }), function (msg) {
    callback("sqlInsert", msg);
  });
}

/**
 * 修改数据库字段(Change))
 *
 * @export
 * @param {*} tableName
 * @param {*} m_cid
 * @param {*} cons_no
 */
export function sqlUpdata(tableName, m_cid, cons_no, contentValues, callback) {
  AndroidApi.update(JSON.stringify({
    tableName: tableName,
    whereClause: "M_CID = ? AND CONS_NO = ?",
    whereArgs: [m_cid, cons_no],
    contentValues: contentValues
  }), function (msg) {
    callback("sqlUpdata", msg);
  });
}
/**
 * 修改数据库字段
 *
 * @export
 * @param {*} tableName
 * @param {*} m_cid
 * @param {*} cons_no
 */
export function sqlUpdataChange(tableName, _contact, m_cid,cons_no, contentValues, callback) {
  console.log(_contact+"= ? AND CONS_NO = ?");
  AndroidApi.update(JSON.stringify({
    tableName: tableName,
    whereClause: _contact+"= ? AND CONS_NO = ?",
    whereArgs: [m_cid, cons_no],
    contentValues: contentValues
  }), function (msg) {
    console.log(22222222222222222222222222,msg);
    callback("sqlUpdataChange", msg);
  });
}
/**
 * 查询图片
 * 
 * @export
 * @param {*} tableName 表名
 * @param {*} _CID 字段
 * @param {*} m_cid M_CID
 * @param {*} callback 回掉函数
 */
export function sqlFILE_Query(tableName, _CID, m_cid, callback) {
  AndroidApi.queryTableData(
    JSON.stringify({
      whereClause: "SELECT * FROM " + tableName + " A, FILE_RECORDE B WHERE A.FILE_ID=B.FILE_ID AND " + _CID + "=?",
      whereArgs: [m_cid]
    }),
    function (msg) {
      console.log("SELECT * FROM " + tableName + " A, FILE_RECORDE B WHERE A.FILE_ID=B.FILE_ID AND " + _CID + "=" + m_cid);
      console.log(msg);
      callback(msg.result);
    }
  );
}
/**
 * 查询图片
 * 
 * @export
 * @param {*} tableName 表名
 * @param {*} _CID 字段
 * @param {*} m_cid M_CID
 * @param {*} callback 回掉函数
 */
export function sqlFILE_QueryAll(tableName, _CID, m_cid, callback) {
  AndroidApi.queryTableData(
    JSON.stringify({
      whereClause: "SELECT * FROM " + tableName + " A, FILE_RECORDE B WHERE A.FILE_ID=B.FILE_ID AND A." + _CID + "=? AND DIRECTION_TYPE IN (1, 2)",
      whereArgs: [m_cid]
    }),
    function (msg) {
      console.log("SELECT * FROM " + tableName + " A, FILE_RECORDE B WHERE A.FILE_ID=B.FILE_ID AND A." + _CID + "=" + m_cid + " AND DIRECTION_TYPE IN (1, 2)");
      console.log(msg);
      callback(msg.result);
    }
  );
}
/**
 * 添加图片
 *
 * @export
 * @param {*} tableName 表名
 * @param {*} tmpList 
 */
export function sqlFILE_Insert(tableName, tmpList, callback) {
  AndroidApi.insert(JSON.stringify({
    "jToken": "",
    "tableName": tableName,
    "contentValues": tmpList
  }), function (msg) {
    callback("sqlFILE_Insert", msg);
  });
}
/**
 * 删除图片
 *
 * @export
 * @param {*} tableName 表名
 * @param {*} FILE_ID 
 * @param {*} callback 
 */
export function sqlFILE_Delete(tableName, FILE_ID, callback) {
  let delPhoto = {
    "tableName": tableName,
    "whereClause": "FILE_ID=?",
    "whereArgs": [FILE_ID]
  }
  AndroidApi.deletes(JSON.stringify(delPhoto), function (msg) {
    console.log("SC_RST_DTL_Table删除记录:" + JSON.stringify(msg))
    // AndroidApi.alert(JSON.stringify(msg));
    callback("sqlFILE_Delete", msg);
  });
}
/**
 * 扫一扫
 *
 * @export
 * @param {*} field
 * @param {*} callback
 */
export function scanNewMeterAction(field, callback) {
  const _this = this;
  AndroidApi.scanCode({
    "type": "camera"
  }, function (msgJson) {
    callback(field, msgJson.codeStr)
  });
}
/**
 *
 *
 * @export
 * @param {*} photoSrc 图片路径
 * @param {*} successCall
 */
export function deletePhoto(photoSrc, successCall) {
  let delPhoto = {
    "tableName": "FILE_RECORDE",
    "whereClause": "FILE_SRC=?",
    "whereArgs": [photoSrc]
  }
  console.log("delPhoto" + JSON.stringify(delPhoto))
  AndroidApi.deletes(JSON.stringify(delPhoto), function (msg) {
    console.log("SC_RST_DTL_Table删除记录:" + JSON.stringify(msg))
    // AndroidApi.alert(JSON.stringify(msg));
    successCall("删除图片成功！");
  });
}

/**
 * 拍照公用功能
 * @param {*} app_no 工单编号 ""
 * @param {*} cons_no 用户编号
 * @param {*} item_no 照片类型 1202 联系信息 1203 合同信息 1204 开票信息
 * @param {*} busy_no 业务编码 0
 * @param {*} successCall 成功回调
 * @param {*} errorCall 失败回调
 */
export function takePhotoAll(callback) {
  //APP_NO VARCHAR2(16),CONS_NO VARCHAR2(16), FILE_SRC VARCHAR2(128), e2(256), ITEM_NO VARCHAR2(16), BUSY_NO VARCHAR2(16) 
  var takePhoto = {
    "photoType": "ALL",
    "quality": "50"
  };
  console.log("拍照入参" + JSON.stringify(takePhoto))
  AndroidApi.takePhoto(takePhoto, function (photoMsg) {
    callback(photoMsg)
  });
}
export function submitremoval(param, SERVICE_ID, callback) {
  console.log({
    "data": {
      "SID": UserInfo.SID,
      "SN": getsn(),
      "DATA": {
        DUTY_ID: UserInfo.DUTY_ID,
        APP_NO: "",
        ...param
      },
      "SERVICE_ID": SERVICE_ID,
      "USER": UserInfo.USER,
      "PWD": UserInfo.PWD
    }
  });
  CallNet('', '',
    {
      "data": {
        "SID": UserInfo.SID,
        "SN": getsn(),
        "DATA": {
          DUTY_ID: UserInfo.DUTY_ID,
          APP_NO: "",
          ...param
        },
        "SERVICE_ID": SERVICE_ID,
        "USER": UserInfo.USER,
        "PWD": UserInfo.PWD
      }
    },
    function (success) {
      console.log('提交', success);
      //回调
      callback(success);
    },
    function (error) {
      console.log('shibai', error);
      callback(error);
    }
  );
}